<?php

namespace App\Http\Controllers\Admin;

use Validator;
use App\Category;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    public function index(Request $request)
    {
        $categorys = Category::orderBy('fullpath', 'asc')->get();
        return view('admin.category.index', [
            'request' => $request,
            'categorys' => $categorys
        ]);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token', 'parentId']);
            $rules = [
                'name' => 'required|min:2|max:20',
                'sort' => 'required|integer'
            ];
            $messages = [
                'name.required' => '请输入类别名！',
                'name.min' => '类别名至少两个字符！',
                'name.max' => '类别名最多20个字符！',
                'sort.required' => '请输入排序值！',
                'sort.integer' => '排序值必须为整数'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }
            $result = Category::create($data);
            $parentId = $request->input('parent_id');
            if ($parentId == '0') {
                $result->fullpath = $result->id;
            } else {
                $parentCategory = Category::find($parentId);
                $result->fullpath = $parentCategory->fullpath . ',' . $result->id;
            }
            $result->save();
            return redirect('admin/category');
        }
        $categorys = Category::orderBy('fullpath', 'asc')->get();
        return view('admin.category.add', [
            'request' => $request,
            'categorys' => $categorys
        ]);
    }

    public function edit(Request $request, $id)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'name' => 'required|min:2|max:20',
                'sort' => 'required|integer'
            ];
            $messages = [
                'name.required' => '请输入类别名！',
                'name.min' => '类别名至少2个字符！',
                'name.max' => '类别名最多20个字符！',
                'sort.required' => '请输入排序值！',
                'sort.integer' => '排序值必须为整数'
            ];
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }
            $category = Category::find($data['id']);
            $category->name = $data['name'];
            $category->sort = $data['sort'];
            $category->save();
            return redirect('admin/category');
        }
        $category = Category::find($id);
        if (!isset($category->name)) {
            return redirect('admin/category');
        }
        return view('admin.category.edit', [
            'request' => $request,
            'category' => $category
        ]);
    }

    public function del(Request $request)
    {
        $id = $request->input('id');
        $category = Category::find($id);
        $count = Category::where('parent_id', '=', $id)->count();
        if ($count >= 1) {
            $return = ['code' => __LINE__, 'msg' => '删除失败，请先删除此分类下的子分类'];
            return response()->json($return);
        }
        if ($category->delete()) {
            $return = ['code' => 0];
        } else {
            $return = ['code' => __LINE__, 'msg' => '删除失败'];
        }
        return response()->json($return);
    }

}